const wx2my = require('../../wx2my');

// pages/admin/admin.js
const app = getApp();
let page = 1;
let totalpage;
Page({
  data: {
    chooseDoor: true,
    openSus: true,
    repSus: true,
    powerFail: true,
    handFail: true,
    powerBackagain: 'powerBackagain',
    handBackagain: 'handBackagain',
    select: 0,
    select2: 0,
    keyword: '',
    stopPull: '',
    isChecked1: false
  },
  onLoad: function (e) {
    this.setData({
      corp_id: e.corp_id
    });
  },
  onReady: function () {},
  onShow: function () {
    page = 1;
    this.get_list();
  },

  changeSwitch1(e) {
    console.log(e);
    let id = e.currentTarget.dataset.id;
    let ident = e.detail.value == true ? '1' : '0';
    let that = this;
    app.wxRequest('beat_control', {
      'token': wx2my.getStorageSync('token'),
      'ids': id,
      'ident': ident
    }, function (res) {
      console.log(res);

      if (res.data.status == 1) {
        app.toast('操作成功'); // that.get_list();
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  get_list() {
    let that = this;
    app.wxRequest('macList', {
      'token': wx2my.getStorageSync('token'),
      'corp_id': that.data.corp_id,
      'keyword': that.data.keyword,
      'page': page
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let list = res.data.data.list;
        list.forEach((item, index) => {
          item.toy_data = item.toy_data;
          item.toy_data.forEach((items, index1) => {
            items.electric = parseFloat(items.electric);
          });
        });

        if (page == 1) {
          totalpage = res.data.data.totalpage;
        }

        that.setData({
          list: page == 1 ? list : that.data.list.concat(list),
          lock: totalpage <= page ? true : false
        });
      }
    });
  },

  openKon(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    app.wxRequest('openKon/' + id, {
      'token': wx2my.getStorageSync('token')
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.toast("开柜成功");
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  changeRentToUnused(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    app.wxRequest('changeRentToUnused/' + id, {
      'token': wx2my.getStorageSync('token')
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.toast("修改成功");
      } else {
        that.setData({
          select: 0,
          select2: 0
        });
        app.toast(res.data.msg);
      }
    });
  },

  changeUsedToUnused(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    app.wxRequest('changeUsedToUnused/' + id, {
      'token': wx2my.getStorageSync('token')
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.toast("修改成功");
      } else {
        that.setData({
          select: 0,
          select2: 0
        });
        app.toast(res.data.msg);
      }
    });
  },

  openUsed(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    app.wxRequest('openHaveUse/' + id, {
      'token': wx2my.getStorageSync('token')
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.toast("开柜成功");
      } else {
        that.setData({
          select: 0,
          select2: 0
        });
        app.toast(res.data.msg);
      }
    });
  },

  searchInput(e) {
    console.log(e);
    this.setData({
      keyword: e.detail.value
    });

    if (e.detail.value == '') {
      page = 1;
      this.get_list();
    }
  },

  search() {
    let that = this;

    if (that.data.keyword == '') {
      app.toast('请输入设备号或地址');
    } else {
      page = 1;
      that.get_list();
    }
  },

  toDetail(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    let macno = e.currentTarget.dataset.macno;
    console.log(id); // wx.navigateTo({
    //   url: '/pages/device_detail/device_detail?id='+id+'&macno='+macno,
    // })
  },

  replenish(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    app.wxRequest('macMending/' + id, {
      'token': wx2my.getStorageSync('token')
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let yun_id = res.data.data.yun_id;
        that.setData({
          yun_id: yun_id,
          openSus: false,
          chooseDoor: true
        });
        setTimeout(() => {
          that.get_pull();
        }, 2000);
      } else {
        that.setData({
          select: 0,
          select2: 0
        });
        app.toast(res.data.msg);
      }
    });
  },

  openDoor(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    app.wxRequest('openDoor/' + id, {
      'token': wx2my.getStorageSync('token')
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.setData({
          openSus: false,
          chooseDoor: true
        });
      } else {
        that.setData({
          select: 0,
          select2: 0
        });
        app.toast(res.data.msg);
      }
    });
  },

  //是否开门成功
  get_openDoor() {},

  //开门轮询
  get_pull() {
    let that = this;
    that.setData({
      openSus: true
    });
    app.wxRequest('Mending_Polling', {
      'yun_id': that.data.yun_id
    }, function (res) {
      console.log(res);

      if (res.data.code == 100) {
        that.setData({
          repSus: false
        });
        wx2my.hideLoading();
      } else if (res.data.code == 22) {
        wx2my.showLoading({
          title: '补货中',
          mask: true
        });
        that.data.stopPull = setTimeout(function () {
          that.get_pull();
        }, 2000);
      } else if (res.data.code == -50) {
        that.setData({
          handFail: false
        });
        wx2my.hideLoading();
      } else {
        that.setData({
          powerFail: false
        });
        wx2my.hideLoading();
      }
    });
  },

  del() {
    this.setData({
      chooseDoor: true,
      openSus: true
    });
  },

  one() {
    let that = this;
    that.setData({
      chooseDoor: false,
      select: 1,
      box: 1
    });
    that.get_openDoor();
  },

  two() {
    let that = this;
    that.setData({
      chooseDoor: false,
      select2: 2,
      box: 2
    });
    that.get_openDoor();
  },

  powerBackagain() {
    let that = this;
    that.setData({
      powerFail: true
    });
    that.get_openDoor();
  },

  handBackagain() {
    let that = this;
    that.setData({
      handFail: true
    });
    that.get_openDoor();
  },

  toSus() {
    let that = this;
    that.setData({
      repSus: true,
      select: 0,
      select2: 0
    });
    page = 1;
    that.get_list();
  },

  onHide: function () {},
  onUnload: function () {
    let that = this;
    clearTimeout(that.data.stopPull);
    that.setData({
      stopPull: ''
    });
  },
  onPullDownRefresh: function () {},
  onReachBottom: function () {
    page++;

    if (!this.data.lock) {
      this.get_list();
    }
  },
  onShareAppMessage: function () {}
});